<?php
class ControllerInformationFriend extends Controller {
	private $error = array();
	public function index() {
		$this->language->load('information/contact');
		
		$this->document->setTitle('Friend Sites');
		
		$this->load->model('catalog/information');
		
		if (($this->request->server['REQUEST_METHOD'] == 'POST')) {
			if ($this->session->data['captcha'] == $this->request->post['captcha']) {
				$this->model_catalog_information->insetFriednLink($this->request->post);
				$this->redirect($this->url->link('information/friend/success'));
			}
			else {
				$this->data['error_captcha'] = "Verification code does not match the image!";
			}
			
			// 			$mail = new Mail();
			// 			$mail->protocol = $this->config->get('config_mail_protocol');
			// 			$mail->parameter = $this->config->get('config_mail_parameter');
			// 			$mail->hostname = $this->config->get('config_smtp_host');
			// 			$mail->username = $this->config->get('config_smtp_username');
			// 			$mail->password = $this->config->get('config_smtp_password');
			// 			$mail->port = $this->config->get('config_smtp_port');
			// 			$mail->timeout = $this->config->get('config_smtp_timeout');
			// 			$mail->setTo($this->config->get('config_email'));
			// 			$mail->setFrom($this->request->post['email']);
			// 			$mail->setSender($this->request->post['name']);
			// 			$mail->setSubject(html_entity_decode(sprintf($this->language->get('email_subject'), $this->request->post['name']), ENT_QUOTES, 'UTF-8'));
			// 			$mail->setText(strip_tags(html_entity_decode($this->request->post['enquiry'], ENT_QUOTES, 'UTF-8')));
			// 			$mail->send();
		}
		
		$this->data['breadcrumbs'] = array();
		
		$this->data['breadcrumbs'][] = array( 
			'text' => $this->language->get('text_home'), 
			'href' => $this->url->link('common/home'), 
			'separator' => false 
		);
		
		$this->data['breadcrumbs'][] = array( 
			'text' => $this->language->get('heading_title'), 
			'href' => $this->url->link('information/contact'), 
			'separator' => $this->language->get('text_separator') 
		);
		
		if (isset($this->request->get['page'])) {
			$page = $this->request->get['page'];
		}
		else {
			$page = 1;
		}
		
		$limit = 10;
		
		$this->data['heading_title'] = 'Friend Sites';
		
		$this->data['friends'] = $this->model_catalog_information->getFriendLink($page, $limit);
		
		$this->data['button_continue'] = $this->language->get('button_continue');
		
		$this->data['action'] = $this->url->link('information/friend');
		
		$this->data['friend_total'] = $this->model_catalog_information->getTotalFriendLink();
		
		$pagination = new Pagination();
		$pagination->total = $this->data['friend_total'];
		$pagination->page = $page;
		$pagination->limit = $limit;
		$pagination->text = $this->language->get('text_pagination');
		$pagination->url = $this->url->link('sale/order/history', 'token=' . $this->session->data['token'] . '&page={page}', 'SSL');
		
		$this->data['pagination'] = $pagination->render();
		
		if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/information/contact.tpl')) {
			$this->template = $this->config->get('config_template') . '/template/information/friend_link.tpl';
		}
		else {
			$this->template = 'default/template/information/friend_link.tpl';
		}
		
		$this->children = array( 
			'common/content_top', 
			'common/content_bottom', 
			'common/footer', 
			'common/header' 
		);
		
		$this->response->setOutput($this->render());
	}
	public function success() {
		$this->language->load('information/contact');
		
		$this->document->setTitle('Friend Sites');
		
		$this->data['breadcrumbs'] = array();
		
		$this->data['breadcrumbs'][] = array( 
			'text' => $this->language->get('text_home'), 
			'href' => $this->url->link('common/home'), 
			'separator' => false 
		);
		
		$this->data['breadcrumbs'][] = array( 
			'text' => $this->language->get('heading_title'), 
			'href' => $this->url->link('information/contact'), 
			'separator' => $this->language->get('text_separator') 
		);
		
		$this->data['heading_title'] = 'Friend Sites';
		
		$this->data['text_message'] = $this->language->get('text_message');
		
		$this->data['button_continue'] = $this->language->get('button_continue');
		
		$this->data['continue'] = $this->url->link('common/home');
		
		if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/common/success.tpl')) {
			$this->template = $this->config->get('config_template') . '/template/common/success.tpl';
		}
		else {
			$this->template = 'default/template/common/success.tpl';
		}
		
		$this->children = array( 
			'common/column_left', 
			'common/column_right', 
			'common/content_top', 
			'common/content_bottom', 
			'common/footer', 
			'common/header' 
		);
		
		$this->response->setOutput($this->render());
	}
	private function validate() {
		if ((utf8_strlen($this->request->post['name']) < 3) || (utf8_strlen($this->request->post['name']) > 32)) {
			$this->error['name'] = $this->language->get('error_name');
		}
		
		if (!preg_match('/^[^\@]+@.*\.[a-z]{2,6}$/i', $this->request->post['email'])) {
			$this->error['email'] = $this->language->get('error_email');
		}
		
		if ((utf8_strlen($this->request->post['enquiry']) < 10) || (utf8_strlen($this->request->post['enquiry']) > 3000)) {
			$this->error['enquiry'] = $this->language->get('error_enquiry');
		}
		
		if (empty($this->session->data['captcha']) || ($this->session->data['captcha'] != $this->request->post['captcha'])) {
			$this->error['captcha'] = $this->language->get('error_captcha');
		}
		
		if (!$this->error) {
			return true;
		}
		else {
			return false;
		}
	}
}
?>
